/**
 * 作者: hxl
 * 日期: 2025-05-01 22:40:48
 * 备注: 联盟成就表
 */
package com.motu.monstercity.server.game.userdata;

import com.motu.monstercity.protocol.Common.PBUnionHonor;
import com.motu.vertx.module.utility.model.BaseModel;
import com.motu.vertx.module.utility.model.Model;
import io.vertx.core.json.JsonObject;
import io.vertx.core.json.JsonArray;

public class UnionHonorModel extends BaseModel implements Model {
    public static final String LISTKEY = "union_id.honor_type";
    public static final int REDIS_EXPIRE_TIME = 0;// redis过期时间
    public static final int UPDATE_DB_TYPE = UPDATE_DB_TYPE_QUEUE;// 数据入库类型 队列入库
    public static final boolean ORDER_LIST = false;// 是否需要额外的有序列表

    protected long id; // id
    protected long unionId; // 联盟id
    protected int honorType; // 成就类型
    protected long num; // 目前达成的数量
    protected String getReward = ""; // 已领取的奖励

    public void reset() {
    }

    protected String getKey() {
        return "UnionHonor." + unionId + "." + honorType;
    }

    protected String getListKey() {
        return "UnionHonor." + unionId;
    }

    protected String getListItemKey() {
        return honorType + "";
    }

    public void doCreate() {
        setTableId();
        insert();
    }

    public void setTableId() {
        putId(incrTableId("union_honor"));
    }

    public JsonObject insert() {
        return super.insert(getKey(), id, REDIS_EXPIRE_TIME, UPDATE_DB_TYPE, LISTKEY, getListKey(), getListItemKey(), ORDER_LIST);
    }

    public void delete() {
        super.delete(getKey(), id, UPDATE_DB_TYPE, 0, LISTKEY, getListKey(), getListItemKey(), ORDER_LIST);
    }

    /**
     * 根据入库类型更新
     */
    public JsonObject update() {
        return super.update(getKey(), id, REDIS_EXPIRE_TIME, UPDATE_DB_TYPE, 0, getAllObj(), LISTKEY, getListKey(), getListItemKey());
    }

    /**
     * 实时入队列
     */
    public JsonObject updateQueueNow() {
        return super.update(getKey(), id, REDIS_EXPIRE_TIME, UPDATE_DB_TYPE, 0, getAllObj(), LISTKEY, getListKey(), getListItemKey());
    }

    public JsonObject getAllObj(){
        JsonObject obj = new JsonObject();
        obj.put("id", id); // id
        obj.put("unionId", unionId); // 联盟id
        obj.put("honorType", honorType); // 成就类型
        obj.put("num", num); // 目前达成的数量
        obj.put("getReward", getReward); // 已领取的奖励
        return obj;
    }

    public PBUnionHonor.Builder takeInitBuilder() {
        PBUnionHonor.Builder sb = PBUnionHonor.newBuilder();
        if (honorType != 0) {
            sb.setHonorType(honorType);
        }
        if (num != 0) {
            sb.setNum(num);
        }
        if (this.getReward != null && this.getReward.length() > 0) {
            JsonArray list = new JsonArray(this.getReward);
            for(int i=0; i<list.size(); i++) {
                sb.addGetReward(list.getInteger(i));
            }
        }
        return sb;
    }

    public PBUnionHonor.Builder takeBuilder() {
        if (updateObj == null || updateObj.isEmpty()) {
            return takeInitBuilder();
        }
        PBUnionHonor.Builder sb = PBUnionHonor.newBuilder();
        if (isFieldUpdate("honorType")) {
            sb.setHonorType(honorType);
        }
        if (isFieldUpdate("num")) {
            sb.setNum(num);
        }
        if (isFieldUpdate("getReward")) {
            if (this.getReward != null && this.getReward.length() > 0) {
                JsonArray list = new JsonArray(this.getReward);
                for(int i=0; i<list.size(); i++) {
                    sb.addGetReward(list.getInteger(i));
                }
                if (list.isEmpty()) {
                    sb.clearGetReward();
                }
            } else {
                sb.clearGetReward();
            }
        }
        return sb;
    }

    public long getId() {
        return id;
    }

    public void putId(long id) {
        if (this.id != id) {
            this.id = id;
            updateRedisHashField(getKey(), "id", this.id);
        }
    }

    public void addId(long value) {
        this.id += value;
        updateRedisHashField(getKey(), "id", this.id);
    }

    public long getUnionId() {
        return unionId;
    }

    public void putUnionId(long unionId) {
        if (this.unionId != unionId) {
            this.unionId = unionId;
            updateRedisHashField(getKey(), "unionId", this.unionId);
        }
    }

    public void addUnionId(long value) {
        this.unionId += value;
        updateRedisHashField(getKey(), "unionId", this.unionId);
    }

    public int getHonorType() {
        return honorType;
    }

    public void putHonorType(int honorType) {
        if (this.honorType != honorType) {
            this.honorType = honorType;
            updateRedisHashField(getKey(), "honorType", this.honorType);
        }
    }

    public void addHonorType(int value) {
        this.honorType += value;
        updateRedisHashField(getKey(), "honorType", this.honorType);
    }

    public long getNum() {
        return num;
    }

    public void putNum(long num) {
        if (this.num != num) {
            this.num = num;
            updateRedisHashField(getKey(), "num", this.num);
        }
    }

    public void addNum(long value) {
        this.num += value;
        updateRedisHashField(getKey(), "num", this.num);
    }

    public JsonArray getGetRewardArray() {
        if (this.getReward != null && this.getReward.length() > 0) {
            return new JsonArray(this.getReward);
        } else {
            return new JsonArray();
        }
    }

    public String getGetReward() {
        return getReward;
    }

    public void putGetReward(String getReward) {
        if (!this.getReward.equals(getReward)) {
            this.getReward = getReward;
            updateRedisHashField(getKey(), "getReward", this.getReward);
        }
    }

}